package net.pws.common.util;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class StringUtils extends org.apache.commons.lang.StringUtils {
    
    public static BigDecimal convert2BigDecimal(String value) {
        if (isEmpty(value)) {
            return null;
        }
        try {
            return new BigDecimal(value);
        }
        catch (RuntimeException e) {
            return null;
        }
    }
    
    public static Long convert2Long(String value) {
        if (isEmpty(value)) {
            return null;
        }
        try {
            return new Long(value);
        }
        catch (RuntimeException e) {
            return null;
        }
    }
    
    public static String replace(String resource,
                                 String[] oldstrs,
                                 String[] newStrs) {
        for (int i = 0; i < oldstrs.length; i++) {
            resource = StringUtils.replace(resource, oldstrs[i], newStrs[i]);
        }
        return resource;
    }
    
    // =========================字符串替换
    /**
     * 将字符串resource中的子字符串oldstr用新字符串newstr代替
     * 
     * @param resource
     * @param oldstr
     * @param newstr
     * @return
     */
    public static String replace(String resource, String oldstr, String newstr) { // 用新字符串替换字符串中的旧字符串。
        StringBuffer sbuf = new StringBuffer();
        try {
            if ((oldstr == null)) {
                return ""; // 异常
            }
            if ((resource == null)) {
                return ""; // 异常
            }
            if ((newstr == null)) {
                return ""; // 异常
            }
            if (resource.indexOf(oldstr) == -1) {
                return resource; // 没有要替换内容，返回原来的串
            }
            int start = 0;
            int end = 0;
            String rest = "";
            end = resource.indexOf(oldstr);
            String left = "";
            while (end != -1) {
                left = resource.substring(start, end);
                sbuf.append(left);
                sbuf.append(newstr);
                rest = resource.substring(end + oldstr.length());
                resource = rest;
                end = resource.indexOf(oldstr);
            }
            sbuf.append(rest);
        }
        catch (Exception e) {
            e.printStackTrace();
            return ""; // 异常
        }
        return sbuf.toString();
    }
    
    public static String dateToString(java.util.Date date) { // 按格式转换日期字符串
        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return bartDateFormat.format(date);
    }
    
    public static String dateToString(java.util.Date date, String format) { // 按格式转换日期字符串
        SimpleDateFormat bartDateFormat = new SimpleDateFormat(format);
        return bartDateFormat.format(date);
    }
    
    public static String timeToString(java.util.Date date) { // 按格式转换日期字符串
        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return bartDateFormat.format(date);
    }
    
    // =========================字符串替换
    /**
     * 将字符串resource中的子字符串oldstr用新字符串newstr代替
     * 
     * @param resource
     * @param oldstr
     * @param newstr
     * @return
     */
    public static String Replace(String resource, String oldstr, String newstr) { // 用新字符串替换字符串中的旧字符串。
        StringBuffer sbuf = new StringBuffer();
        try {
            if ((oldstr == null)) {
                return ""; // 异常
            }
            if ((resource == null)) {
                return ""; // 异常
            }
            if ((newstr == null)) {
                return ""; // 异常
            }
            if (resource.indexOf(oldstr) == -1) {
                return resource; // 没有要替换内容，返回原来的串
            }
            int start = 0;
            int end = 0;
            String rest = "";
            end = resource.indexOf(oldstr);
            String left = "";
            while (end != -1) {
                left = resource.substring(start, end);
                sbuf.append(left);
                sbuf.append(newstr);
                rest = resource.substring(end + oldstr.length());
                resource = rest;
                end = resource.indexOf(oldstr);
            }
            sbuf.append(rest);
        }
        catch (Exception e) {
            e.printStackTrace();
            return ""; // 异常
        }
        return sbuf.toString();
    }
    
    /**
     * 处理非法的SQL条件,由于 in()条件的长度不能大于1000,但是本系统中有这种情况,则需要将该SQL条件拆分成小的SQL条件然后做与运算
     * 
     * @param conditionField
     * @param conditionValue
     *            用','分割开的字符串
     * @return
     */
    public static String transferSql(String conditionField,
                                     String conditionValue) {
        String conditionStr = "";
        int index = 0;
        for (int i = 0; i < conditionValue.length(); i++) {
            index++;
            if ((index > 900 && conditionValue.charAt(i) == ',') || i == conditionValue.length() - 1) {
                String subConditionValue = "";
                if (i == conditionValue.length() - 1)
                    subConditionValue = conditionValue.substring(i - index + 1,
                                                                 i + 1);
                else
                    subConditionValue = conditionValue.substring(i - index + 1,
                                                                 i - 1);
                if (conditionStr.equals("")) {
                    conditionStr += "(" + conditionField
                                    + " in ("
                                    + subConditionValue
                                    + ")";
                }
                else
                    conditionStr += " or " + conditionField
                                    + " in ("
                                    + subConditionValue
                                    + ")";
                index = 0;
            }
        }
        if (!conditionStr.equals("")) {
            conditionStr += ")";
        }
        return conditionStr;
    }
    
    /**
     * 处理非法的SQL条件,由于 in()条件的长度不能大于1000,但是本系统中有这种情况,则需要将该SQL条件拆分成小的SQL条件然后做与运算
     * 
     * @param conditionField
     * @param conditionValue
     *            用','分割开的字符串
     * @return
     */
    public static String transferHql(String conditionField,
                                     String conditionValue,
                                     Map<String, Object> parametersMap) {
        String condition = "";
        int index = 0;
        for (int i = 0; i < conditionValue.length(); i++) {
            index++;
            if ((index > 900 && conditionValue.charAt(i) == ',') || i == conditionValue.length() - 1) {
                String subConditionValue = "";
                if (i == conditionValue.length() - 1)
                    subConditionValue = conditionValue.substring(i - index + 1,
                                                                 i + 1);
                else
                    subConditionValue = conditionValue.substring(i - index + 1,
                                                                 i - 1);
                // System.out.println("subConditionValue="+subConditionValue);
                if (condition.equals("")) {
                    condition += " and (" + conditionField + " in (:userId)";
                    parametersMap.put("userId", subConditionValue.split(","));
                }
                else {
                    condition += " or " + conditionField + " in (:userId)";
                    parametersMap.put("userId", subConditionValue.split(","));
                }
                index = 0;
            }
        }
        if (!condition.equals("")) {
            condition += ")";
        }
        return condition;
    }
    
    /**
     * 指定字符串是否都为空白字符串
     * 
     * @param strs
     * @return
     */
    public static boolean areBlank(String... strs) {
        if (strs == null || strs.length == 0) {
            return true;
        }
        for (int i = 0; i < strs.length; i++) {
            if (!isBlank(strs[i])) {
                return false;
            }
        }
        return true;
    }
    
    /**
     * 指定字符串是否都为空字符串
     * 
     * @param strs
     * @return
     */
    public static boolean areEmpty(String... strs) {
        if (strs == null || strs.length == 0) {
            return true;
        }
        for (int i = 0; i < strs.length; i++) {
            if (!isEmpty(strs[i])) {
                return false;
            }
        }
        return true;
    }
    
    /**
     * 判断字符是否包含中文
     * @param str
     * @return
     */
    public static boolean hasChineseChar(String str) {
        if (isBlank(str)) {
            return false;
        }
        
        boolean temp = false;
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            temp = true;
        }
        return temp;
    }
}
